<?php
namespace App\Http\Controllers\Flow;

use App\Enum\ClassListPayResultHistoryEnum;
use App\Exceptions\APIHttpException;
use App\Http\Controllers\Controller;
use App\Http\Library\Common;
use App\Http\Library\Excel;
use App\Http\Middleware\Auth;
use App\Http\Model\Flow\ClassListPayResultHistoryModel;
use App\Http\ModelDao\Flow\ClassListPayResultHistoryDao;
/**
 * 班级交易信息推送流水
 *
 * Class ClassTranPushFlow
 * @package App\Http\Controllers\Flow
 */
class ClassTranPushFlow extends Controller
{
    public function classTranPushFlow()
    {
        $where = $this->_combineSearchCondition();
        $page = (int)request()->input('page', 1);
        $pageSize = Common::limit( (int)request()->input('page_size', 30) );

        $businessUnitID = (int) request()->input('business_unit_id');
        $ids = explode(',', Auth::pfs([$businessUnitID]));

        $enablePage = true; //是否启用分页查询
        // 根据条件获取班级交易推送流水
        $classPayHistoryObj = ClassListPayResultHistoryModel::fetchClassPayHistoryData($ids, $where, $enablePage, $page, $pageSize);
        $classPayList = ClassListPayResultHistoryDao::classPayResultHistoryList($classPayHistoryObj);

        // 根据条件统计数据条目
        $classPayHistoryCnt = ClassListPayResultHistoryModel::fetchClassPayHistoryCount($ids, $where);

        $result = ['data' => $classPayList, 'start' => ($page-1) * $pageSize + 1, 'page' => $page,
            'page_size' => $pageSize, 'total' => $classPayHistoryCnt];
        return APIHttpException::success($result);
    }

    /**
     * 导出班级信息推送流水 格式Excel
     *
     */
    public function exportClassTranFlow()
    {
        $where = $this->_combineSearchCondition();

        // 事业部ID
        $businessUnitID = (int) request()->input('business_unit_id');
        $ids = explode(',', Auth::pfs([$businessUnitID]));

        $enablePage = false; //是否启用分页查询 导出Excel只需要按照搜索条件导出即可
        // 根据条件获取班级交易推送流水
        $classPayHistoryObj = ClassListPayResultHistoryModel::fetchClassPayHistoryData($ids, $where, $enablePage, 0, 0);
        // 限制导出的数据不能超过 10万 条
        if (count($classPayHistoryObj) > ClassListPayResultHistoryEnum::EXPORT_MAX_LIMIT) {
            throw new APIHttpException(ClassListPayResultHistoryEnum::EXPORT_MAX_LIMIT_MSG);
        }
        $classPayList = ClassListPayResultHistoryDao::classPayResultHistoryList($classPayHistoryObj);

        $ret = [];
        foreach ($classPayList as & $payValue) {
            $ret[] = array_values($payValue);
        }

        if ($ret) {
            $fileName = date('YmdHis') . '_班级交易信息推送流水.xlsx';

            $classTranFlowTitle = ['推送时间', '城市', '主体', '班级ID', '班级名称',
                '汇总日期（数仓计算时间）', '当日缴费笔数', '当日缴费课程金额', '当日缴费手续费', '当日退费笔数', '当日退费课程金额',
                '当日退费手续费', '当日轧差金额', '截止当日缴费总笔数', '截止当日总缴费金额', '截止当日缴费总手续费', '截止当日退费总笔数',
                '截止当日总退费金额', '截止当日退费总手续费', '推送状态', '描述信息'
                ];

            Excel::$download = true;
            Excel::$title = $classTranFlowTitle;
            Excel::send($ret, $fileName);
            exit;
        }
    }

    private function _combineSearchCondition()
    {
        $startTime = trim(request()->input('start_time'));
        $endTime = trim(request()->input('end_time'));
        $cityName = trim(request()->input('city_name'));
        $mainName = trim(request()->input('main_name')); //主体名称
        $className = trim(request()->input('class_name'));
        $pushStatus = trim(request()->input('push_status'));

        $where = [];
        if ($cityName) {
            $where['city_name'] = $cityName;
        }
        if ($mainName) {
            $where['ledger_subject_name'] = $mainName;
        }
        if ($className) {
            $where['class_name'] = $className;
        }
        if ($pushStatus) {
            $where['result'] = $pushStatus;
        }

        if(!empty($startTime)) {
            $where[] = ['push_time', '>=', dateFrom(urldecode($startTime))];
        }
        if (!empty($endTime)) {
            $where[] = ['push_time', '<=', dateTo(urldecode($endTime))];
        }

        return $where;
    }
}
